// pages/revisedizhi/revisedizhi.js
const {editAddress,shixian} =require('../../util/api')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    date: '2016-09-01',
    time: '12:01',
    memberName:"",
    telNum:"",
    address:"",
    aaa:"",
    multiIndex:[0,0,0],
    multiArray:[],
    yData:{},
    provinceName:'',
    postalCode:'',
    cityName:'',
    cityCode:'',
    countyName:'',
    countyCode:''
  },
name(e){
  this.setData({
    memberName: e.detail.value
  })
},
tel(e){
  this.setData({
    telNum: e.detail.value
})


},
xuanze(){
  this.setData({
    aaa:0
  })
},

  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })   
  },
  address(e){
    this.setData({
      address:e.detail.value
    })
  },
dizhi(){
  if(this.data.telNum.length===11){
    let that = this
    wx.showModal({
      title: '提示',
      content:"是否确认修改？",
      success (res)  {
        if (res.confirm) {
          console.log('用户点击确定')
          let params={
            id:that.data.id,
            memberId:wx.getStorageSync('memberId'),
            memberName:that.data.memberName,
            telNum:that.data.telNum,
            provinceName:that.data.provinceName,
            postalCode:that.data.postalCode,
            cityName:that.data.cityName,
            countyName:that.data.countyName,
            citycode:that.data.cityCode,
            countycode:that.data.countyCode,
            detailInfo:that.data.address,
            isDefault:1
          };
          editAddress(params).then(res=>{
            if(res.code == 200)
            {
              wx.navigateBack({
                delta: 1,
              })
            }
            else
            {
              wx.showToast({
                title: res.msg,
                icon:'none'
              })
            }
            console.log(res)
           }
           )
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  } else
   {
     wx.showToast({
       title: "请输入正确手机号",
       icon:'none'
     })
   }
},

bindcolumnchange(e){
  
  let multiIndex = this.data.multiIndex
  multiIndex[e.detail.column] = e.detail.value
  this.setData({
    multiIndex:multiIndex
  })
  this.changePickerData()
},

changePickerData(){
  let two = []
  let three = []
  let multiArray= this.data.multiArray
  for (let i = 0; i < this.data.yData.data[this.data.multiIndex[0]].children.length; i++) {
    two[i] = this.data.yData.data[this.data.multiIndex[0]].children[i].areaName
  }
  for (let i = 0; i < this.data.yData.data[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children.length; i++) {
    three[i] = this.data.yData.data[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[i].areaName
  }
  multiArray[1] = two
  multiArray[2] = three
  this.setData({
    multiArray:multiArray,
    provinceName:this.data.yData.data[this.data.multiIndex[0]].areaName,
    postalCode:this.data.yData.data[this.data.multiIndex[0]].areaCode,
    cityName:this.data.yData.data[this.data.multiIndex[0]].children[this.data.multiIndex[1]].areaName,
    cityCode:this.data.yData.data[this.data.multiIndex[0]].children[this.data.multiIndex[1]].areaCode,
    countyName:this.data.yData.data[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[this.data.multiIndex[2]].areaName,
    countyCode:this.data.yData.data[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[this.data.multiIndex[2]].areaCode,
  })

},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let list=JSON.parse(options.thisdizhi)
    
    this.setData({
      memberName:list.memberName,
      memberId:list.memberId,
      telNum:list.telNum,
      provinceName:list.provinceName,
      postalCode:list.postalCode,
      cityName:list.cityName,
      cityCode:list.cityCode,
      countyName:list.countyName,
      countyCode:list.countycode,
      detailInfo:list.detailInfo,
      id:list.id
    })
    shixian().then(res=>{

      
      let first = []
      let two = []
      let three = []
      let multiArray=[]
      for (let i = 0; i < res.data.length; i++) {
        first[i] = res.data[i].areaName;
        
      }
      for (let i = 0; i < res.data[0].children.length; i++) {
        two[i] = res.data[0].children[i].areaName
      }
      for (let i = 0; i < res.data[0].children[0].children.length; i++) {
        three[i] = res.data[0].children[0].children[i].areaName
      }
      multiArray[0] = first
      multiArray[1] = two
      multiArray[2] = three
      this.setData({
        multiArray:multiArray,
        yData:res,
      })
     }
     )
    },
    onShow(){
      // this.setData({
      //   multiIndex:list
      // })
      
    }
  })